package com.cabrida.livestar;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.cabrida.livestar.config.Global;
import com.cabrida.livestar.util.BarUtil;
import com.cabrida.livestar.util.L;
import com.cabrida.livestar.util.SharedUtils;

public abstract class BaseActivity extends Activity implements OnClickListener {
	protected Context context;
	public static final String TAG = BaseActivity.class.getSimpleName();
	protected Dialog mProgressDialog = null;
	protected InputMethodManager inputMethodManager;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		AppManager.getInstance().addActivity(this);
		overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
		context = this;
		inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
		findViewById();
		initView();
		// 透明状态栏
//		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
//			Window window = getWindow();
//			// Translucent status bar
//			window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
//					WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
//			BarUtil.setMiuiStatusBarDarkMode(this, true);
//		}
	}
	
	/**
	 * 绑定控件id
	 */
	protected abstract void findViewById();

	/**
	 * 初始化控件
	 */
	protected abstract void initView();

	/**
	 * 通过类名启动Activity
	 * 
	 * @param pClass
	 */
	protected void openActivity(Class<?> pClass) {
		openActivity(pClass, null);
	}

	/**
	 * 通过类名启动Activity，并且含有Bundle数据
	 * 
	 * @param pClass
	 * @param pBundle
	 */
	protected void openActivity(Class<?> pClass, Bundle pBundle) {
		Intent intent = new Intent(this, pClass);
		if (pBundle != null) {
			intent.putExtras(pBundle);
		}
		startActivity(intent);
	}

	/**
	 * 通过Action启动Activity
	 * 
	 * @param pAction
	 */
	protected void openActivity(String pAction) {
		openActivity(pAction, null);
	}

	/**
	 * 通过Action启动Activity，并且含有Bundle数据
	 * 
	 * @param pAction
	 * @param pBundle
	 */
	protected void openActivity(String pAction, Bundle pBundle) {
		Intent intent = new Intent(pAction);
		if (pBundle != null) {
			intent.putExtras(pBundle);
		}
		startActivity(intent);
	}
	
	public void showToast(String content) {
		Toast.makeText(this, content, Toast.LENGTH_SHORT).show();
	}

	public void showToast(int stringId) {
		Toast.makeText(this, getString(stringId), Toast.LENGTH_SHORT).show();
	}

	/** 加载进度条 */
	@SuppressLint("InflateParams")
	public void showProgress(String context) {
		try {
			if (null == mProgressDialog) {
				mProgressDialog = new ProgressDialog(this, R.style.customDialog);
			}
			mProgressDialog.setCanceledOnTouchOutside(false);
			// progressDialog.setIndeterminate(true);
			mProgressDialog.setCancelable(true);
			mProgressDialog.setOnCancelListener(new OnCancelListener() {

				@Override
				public void onCancel(DialogInterface dialog) {
					BaseActivity.this.finish();
				}
			});
			mProgressDialog.setTitle(context);
			mProgressDialog.show();
			LayoutInflater inflater = LayoutInflater.from(this);
			View v = inflater.inflate(R.layout.common_progress, null);
			mProgressDialog.setContentView(v);
			ImageView image = (ImageView) v.findViewById(R.id.progressBar);
			TextView text = (TextView) v.findViewById(R.id.progressText);
			text.setText(context);
			image.setBackgroundResource(R.drawable.common_progress_anim);
			AnimationDrawable anim = (AnimationDrawable) image.getBackground();
			anim.setOneShot(false);
			anim.stop();
			anim.start();
		} catch (Exception e) {
			L.e(TAG, "UncatchException", e.toString());
		}
	}

	public void showProgress() {
		showProgress("正在加载,请稍候...");
	}

	/** 隐藏进度条 */
	public void dismissProgress() {
		try {
			if (null == mProgressDialog) {

			} else {
				mProgressDialog.dismiss();
			}
		} catch (Exception e) {
			L.e(TAG, "UncatchException", e.toString());
		}
	}

	@Override
	public void onClick(View v) {
		hideSoftKeyboard();
	}
	
	/** 收起软键盘*/
	protected void hideSoftKeyboard() {
        if (getWindow().getAttributes().softInputMode != WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN) {
            if (getCurrentFocus() != null)
                inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),
                        InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }
	
	/** 退出登录，清除信息*/
	protected void clearData() {
		SharedUtils.putString(context, "U_id", "");
		SharedUtils.putString(context, "U_nickname",
				"");
		SharedUtils.putString(context, "U_type",
				"");
		SharedUtils.putString(context, "U_token",
				"");
		SharedUtils.putString(context, "TIm_Sig",
				"");
		SharedUtils.putString(context, "headPic",
				"");
	}
}
